package close.mazegame.entity.player;

import box2dLight.ConeLight;
import box2dLight.PointLight;
import close.mazegame.InputHandler;
import close.mazegame.art.tanks.TankTexture;
import close.mazegame.collision.Collidable;
import close.mazegame.entity.EndStone;
import close.mazegame.entity.Tank;
import close.mazegame.entity.effect.SmokeBlack;
import close.mazegame.entity.projectile.Projectile;
import close.mazegame.input.playerinput.PlayerInput.PlayerKeys;
import close.mazegame.level.Level;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;

public class Player extends Tank {
	
	private static final float POINT_LIGHT_BRIGHTNESS = 0.5f;
	private static final float CONE_LIGHT_BRIGHTNESS = 0.8f;
	
	private static final float SMOKE_INTERVAL = 0.2f;
	private static final float SMOKE_DRIFT = 0.5f;
	
	//private PlayerInput controller;

	protected PointLight pointLight;
	protected ConeLight coneLight;
	private boolean atEnd = false;
	private PlayerAttributes attributes;
	
	private float smoke_time = 0.0f;

	public Player(Vector2 pos, Level level) {
		this(pos, level, new PlayerAttributes());
	}

	public Player(Vector2 pos, Level level, PlayerAttributes attributes) {
		super(pos, level, TankTexture.red, true);
		this.attributes = attributes;
		
		fireIntervalMod = 0.2f;

		pointLight = level.addPointLight(body, 64, new Color(1.0f, 1.0f, 1.0f, POINT_LIGHT_BRIGHTNESS), 2.0f, 1.0f);
		coneLight = level.addConeLight(turret.getBody(), 64, new Color(1.0f, 1.0f, 1.0f, CONE_LIGHT_BRIGHTNESS), 20.0f, 1.0f, 30);
	}
	
	@Override
	public void update(float delta) {
		Vector2 dir = InputHandler.playerInput.getMovementDirection();

		Vector2 turretDir = InputHandler.playerInput.getTurretDirection();
		float turretAngle = turretDir.angle();
		super.move(delta, dir, turretAngle);

		if (InputHandler.playerInput.isPressed(PlayerKeys.FIRE)) {
			fireProjectile();
		}
		
		if (attributes.getCurrentHealth() <= 1) {
			smoke_time += delta;
			if (smoke_time > SMOKE_INTERVAL) {
				level.add(new SmokeBlack(getPositionEngine(), getLevel(), SMOKE_DRIFT));
				smoke_time -= SMOKE_INTERVAL;
			}
		}
		
		if (InputHandler.playerInput.isTyped(PlayerKeys.TOGGLE_FIXTURRET)) {
			if (turret.isFixed()) {
				turret.setFixed(false);
			} else {
				turret.setFixed(true);
			}
		}
	}
	
	public void dispose() {
		super.dispose();
	}

	public boolean isAtEnd() {
		return atEnd;
	}

	public PlayerAttributes getAttributes() {
		return attributes;
	}

	@Override
	protected short getCategoryBits() {
		return EntityCategory.PLAYER.getBits();
	}

	@Override
	public void startContact(Collidable other) {
		if (other instanceof EndStone) {
			atEnd = true;
		}
		if (other instanceof Projectile) {
			attributes.changeHealth(-1);
		}
	}

	@Override
	public void endContact(Collidable other) {
		if (other instanceof EndStone) {
			atEnd = false;
		}
	}

}
